#!/bin/bash
###
#    videometadata
#    version 1.0.1
#    Author: Christoph Holzbaur (Webmaaschter@gmx.de)
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.


# This script makes two things
#
# 1. Lets assume you have an Series-Folder, and
#    every Subfoder of this folder is full with Episodes of one Serie.
#    The script makes Thumbnails of every videofile in every subfolder and
#    updates the Database
#
# 2. Lets assume you have an Rated-Video-Folder, and
#    it is full with stuff your children shouldn't see.
#    The script updates the showlevel of all these files so you have
#    to enter an PIN to see these Files
#
##############################################


#####  Settings #####

### 1. General settings
# DIR where the folders with the Series are
SERIEN_DIR=/mnt/sata/mythtv/videos/Sarjat

# Where are your rated videos
RATED_DIR=/mnt/sata/mythtv/videos/Parental_Advisory
# Which showlevel should your rated Videos have
SHOWLEVEL=4

# Which suffix does your Videos have
SUFFIX=( avi AVI mpg MPG mpeg MPEG mkv MKV ogm OGM wmv WMV )



## 2. Thumbnail-Settings
# Where shold the thumbs go
THUMB_DIR=/mnt/sata/mythtv/covers

# set if you want to make thumbnail of your videos in your RATED_DIR
RATED_THUMBS=true

# at which timepoin i should make the thumbnail
THUMB_TIME=00:01:00



### mysql-settings
HOST=localhost
USER=mythtv
PASSW=mythtv
DATABASE=mythconverg





###### nothig to configure
for SUFF in  ${SUFFIX[*]}
do
  for SERIE in $SERIEN_DIR/*
 do
  if [ -d "$SERIE" ];then
   for DATEI in "$SERIE"/*.$SUFF
   do
    if [ -e "$DATEI" ]
    then
     THUMB_PATH="$THUMB_DIR/`basename "$DATEI"`.png"
     if [ ! -f "$THUMB_PATH" ]
     then
      mplayer -ss $THUMB_TIME -nosound -frames 3 -vo png "$DATEI" && mv 00000001.png "$THUMB_PATH";rm 0000000?.png
      if [ $(which foldericon 2>/dev/null) ]; then
       if [ ! -f "${SERIE}/.directory" ]; then
        pushd "$SERIE"
         foldericon "$THUMB_PATH"
        popd
       fi
      fi
     fi
     echo "UPDATE  videometadata SET coverfile=\"$THUMB_PATH\"  WHERE filename=\"${DATEI}\" ;" | mysql -u $USER --password=$PASSW -D $DATABASE -h $HOST
    fi
   done
  fi
 done
done

if [ "$RATED_THUMBS" = true ];then
 for SUFF in  ${SUFFIX[*]}
 do
  for DATEI in "$RATED_DIR"/*.$SUFF
  do
   if [ -e  "$DATEI" ]
   then
    THUMB_PATH="$THUMB_DIR/`basename "$DATEI"`.png"
    if [ ! -f "$THUMB_PATH" ]
    then
     mplayer -ss $THUMB_TIME -nosound -frames 3 -vo png "$DATEI" && mv 00000001.png "$THUMB_PATH";rm 0000000?.png
    fi
    echo "UPDATE  videometadata SET coverfile=\"$THUMB_PATH\"  WHERE filename=\"${DATEI}\" ;" | mysql -u $USER --password=$PASSW -D $DATABASE -h $HOST
   fi
  done
 done
fi


echo "UPDATE  videometadata SET showlevel=$SHOWLEVEL  WHERE filename LIKE '$RATED_DIR/%' ;" | mysql -u $USER --password=$PASSW -D $DATABASE -h $HOST
